﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ChoWorkflowServer.Model;
using Cinchoo.Core;
using System.Windows.Interop;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Win32;
using Cinchoo.Core.WPF;
using Cinchoo.Core.Windows.Forms;

namespace ChoWorkflowDashboard
{
    /// <summary>
    /// Interaction logic for AddOrEditHolidayCalWindow.xaml
    /// </summary>
    public partial class AddOrEditHolidayCalWindow : ChoBaseWindowEx
    {
        private string _defaultExt = ".wfc";
        private string _filter = "Workflow calendars|*.wfc";
        private int _selectedItemIndex = 0;
        private readonly IChoDataModel _dataModel = ChoDataModelFactory.New();
        private List<ChoAppointment> _myAppointmentsList = new List<ChoAppointment>();
        private ChoTasksDataSet.CHO_HOLIDAY_CALENDAR_DATESDataTable _holidayCalendarDatesTable;
        public ChoTasksDataSet.CHO_HOLIDAY_CALENDARSDataTable HolidayCalendarTable
        {
            get;
            set;
        }

        private bool _isDataUpdated;
        internal bool IsDataUpdated
        {
            get { return _isDataUpdated; }
            set
            {
                if (value)
                    _isDataUpdated = value;
            }
        }

        public string AuditInfo
        {
            get
            {
                if (lstHolidayCals.SelectedItem == null)
                    return null;

                ChoTasksDataSet.CHO_HOLIDAY_CALENDARSRow row = ((DataRowView)lstHolidayCals.SelectedItem).Row as ChoTasksDataSet.CHO_HOLIDAY_CALENDARSRow;
                if (row == null) return null;

                return "Created By: {1}{0}Created Date: {2}{0}Modified By: {3}{0}Modified Date: {4}".FormatString(
                    Environment.NewLine, row.CREATED_BY, row.CREATED_DATE, row.MODIFIED_BY, row.MODIFIED_DATE);
            }
        }

        public AddOrEditHolidayCalWindow()
        {
            InitializeComponent();
            AptCalendar.AppointmentMouseMoved += new MonthViewControl.AppointmentMouseMovedEventHandler(AptCalendar_AppointmentMouseMoved);
            Refresh();
            this.DataContext = this;

            ComponentDispatcher.ThreadIdle += new EventHandler(ComponentDispatcher_ThreadIdle);
        }

        protected override void Refresh()
        {
            using (new ChoWaitCursor())
            {
                _selectedItemIndex = lstHolidayCals.SelectedIndex;

                HolidayCalendarTable = _dataModel.GetAllHolidayCalendars();
                ChoTasksDataSet.CHO_HOLIDAY_CALENDARSRow nullCalendar = (from row in HolidayCalendarTable.AsEnumerable<ChoTasksDataSet.CHO_HOLIDAY_CALENDARSRow>()
                                                                         where row[HolidayCalendarTable.CAL_NAMEColumn, DataRowVersion.Original] != DBNull.Value
                                                                         && (string)row[HolidayCalendarTable.CAL_NAMEColumn, DataRowVersion.Original] == ChoCalendar.NULL_CALENDAR_NAME
                                                                         select row).FirstOrDefault();
                if (nullCalendar != null)
                    HolidayCalendarTable.Rows.Remove(nullCalendar);
                BindingOperations.GetBindingExpressionBase(lstHolidayCals, ListBox.ItemsSourceProperty).UpdateTarget();

                if (_selectedItemIndex >= 0)
                    lstHolidayCals.SelectedIndex = _selectedItemIndex;
            }
        }

        private void ComponentDispatcher_ThreadIdle(object sender, EventArgs e)
        {
            btnDeleteHolidayCal.IsEnabled = lstHolidayCals.SelectedIndex >= 0;
            grpHolidayCalendar.IsEnabled = lstHolidayCals.SelectedIndex >= 0;
            btnExport.IsEnabled = lstHolidayCals.SelectedIndex >= 0;
            btnClone.IsEnabled = lstHolidayCals.SelectedIndex >= 0;
            btnNewHolidayCal.IsEnabled = txtHolidayCalName.Text.Length > 0;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            _promptSaveChanges = false;
            DialogResult = true;
            //this.Close();
        }

        private bool UpdateCalendar()
        {
            try
            {
                ChoTasksDataSet.CHO_HOLIDAY_CALENDARSDataTable calendarTable = HolidayCalendarTable != null ? HolidayCalendarTable.GetChanges() as ChoTasksDataSet.CHO_HOLIDAY_CALENDARSDataTable : null;
                ChoTasksDataSet.CHO_HOLIDAY_CALENDAR_DATESDataTable calendarDatesTable = _holidayCalendarDatesTable != null ? _holidayCalendarDatesTable.GetChanges() as ChoTasksDataSet.CHO_HOLIDAY_CALENDAR_DATESDataTable : null;

                _dataModel.UpdateHolidayCalendar(calendarTable, calendarDatesTable, ChoUser.CurrentUser, Environment.MachineName);

                AcceptChanges();

                //Refresh();

                return true;
            }
            catch (Exception ex)
            {
                RejectChanges();
                MessageBox.Show(ex.Message, Application.Current.MainWindow.Title, MessageBoxButton.OK, MessageBoxImage.Error);
                return false;
            }
        }

        private void lstHolidayCals_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (HasChanges)
            {
                if (!UpdateCalendar())
                    RejectChanges();
            }

            if (lstHolidayCals.SelectedItem == null)
            {
                if (lstHolidayCals.Items.Count > 0)
                    lstHolidayCals.SelectedIndex = 0;

                return;
            }

            _myAppointmentsList.Clear();

            ChoTasksDataSet.CHO_HOLIDAY_CALENDARSRow row = ((DataRowView)lstHolidayCals.SelectedItem).Row as ChoTasksDataSet.CHO_HOLIDAY_CALENDARSRow;
            if (row == null) return;

            txtHolidayCalDesc.ToolTip = AuditInfo;

            //if (row.CAL_ID == 1)
            //{
            //    AptCalendar.IsEnabled = false;
            //}
            //else
            //{
                AptCalendar.IsEnabled = true;

                _holidayCalendarDatesTable = GetHolidayCalendarDatesTable(row.CAL_ID);
                foreach (ChoTasksDataSet.CHO_HOLIDAY_CALENDAR_DATESRow dateRow in _holidayCalendarDatesTable)
                {
                    _myAppointmentsList.Add(ToAppointmentObject(dateRow));
                }
            //}
            SetAppointments();
        }

        //private Dictionary<int, ChoTasksDataSet.CHO_HOLIDAY_CALENDAR_DATESDataTable> _holidayCalendarDatesTableCache = new Dictionary<int, ChoTasksDataSet.CHO_HOLIDAY_CALENDAR_DATESDataTable>();
        private ChoTasksDataSet.CHO_HOLIDAY_CALENDAR_DATESDataTable GetHolidayCalendarDatesTable(int calId)
        {
            return _dataModel.GetAllHolidayCalendarDatesById(calId);
            //if (!_holidayCalendarDatesTableCache.ContainsKey(calId))
            //{
            //    _holidayCalendarDatesTableCache.Add(calId, _dataModel.GetAllHolidayCalendarDatesById(calId));
            //}
            //return _holidayCalendarDatesTableCache[calId];
        }

        private static ChoAppointment ToAppointmentObject(ChoTasksDataSet.CHO_HOLIDAY_CALENDAR_DATESRow dateRow)
        {
            ChoAppointment apt = new ChoAppointment();
            apt.AppointmentID = dateRow;
            apt.StartTime = dateRow.CAL_DATE;
            apt.EndTime = apt.StartTime;
            apt.Subject = dateRow.IsCAL_DATE_DESCNull() ? String.Empty : dateRow.CAL_DATE_DESC;
            return apt;
        }

        private void btnNewHolidayCal_Click(object sender, RoutedEventArgs e)
        {
            if (txtHolidayCalName.Text.Trim().Length == 0) return;

            ChoTasksDataSet.CHO_HOLIDAY_CALENDARSRow row = null;
            try
            {
                using (new ChoWaitCursor())
                {
                    Refresh();
                    var x = (from row1 in HolidayCalendarTable.AsEnumerable<ChoTasksDataSet.CHO_HOLIDAY_CALENDARSRow>()
                             where row1[HolidayCalendarTable.CAL_NAMEColumn, DataRowVersion.Original] != DBNull.Value
                            && (string)row1[HolidayCalendarTable.CAL_NAMEColumn, DataRowVersion.Original] == txtHolidayCalName.Text
                             select row1).FirstOrDefault();
                    if (x != null) return;

                    row = HolidayCalendarTable.NewCHO_HOLIDAY_CALENDARSRow();
                    row.CAL_ID = -1;
                    row.CAL_NAME = txtHolidayCalName.Text.Trim();
                    row.CREATED_BY = ChoUser.CurrentUser;
                    row.CREATED_DATE = DateTime.Now;
                    row.MODIFIED_BY = ChoUser.CurrentUser;
                    row.MODIFIED_DATE = DateTime.Now;
                    //row.CAL_DESC = txtHolidayCalDesc.Text.Trim();
                    HolidayCalendarTable.AddCHO_HOLIDAY_CALENDARSRow(row);

                    ChoTasksDataSet.CHO_HOLIDAY_CALENDARSDataTable calendarTable = HolidayCalendarTable.GetChanges() as ChoTasksDataSet.CHO_HOLIDAY_CALENDARSDataTable;
                    _dataModel.UpdateHolidayCalendar(calendarTable, null, ChoUser.CurrentUser, Environment.MachineName);

                    //ChoMainWindow.SendCalendarCommand(txtHolidayCalName.Text.Trim(), ChoOperationType.Add);

                    txtHolidayCalName.Text = String.Empty;
                    //txtHolidayCalDesc.Text = String.Empty;
                    lstHolidayCals.SelectedIndex = lstHolidayCals.Items.Count - 1;
                    e.Handled = true;
                }
            }
            catch (Exception ex)
            {
                if (row != null)
                    HolidayCalendarTable.Rows.Remove(row);
                MessageBox.Show(ex.Message, Application.Current.MainWindow.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }

        private void btnDeleteHolidayCal_Click(object sender, RoutedEventArgs e)
        {
            if (lstHolidayCals.SelectedItem == null)
                return;

            object selectedItem = lstHolidayCals.SelectedItem;
            ChoTasksDataSet.CHO_HOLIDAY_CALENDARSRow row = ((DataRowView)selectedItem).Row as ChoTasksDataSet.CHO_HOLIDAY_CALENDARSRow;
            if (row == null) return;

            if (MessageBox.Show("Are you sure you want to delete the '{0}' calendar?".FormatString(row.CAL_NAME), Application.Current.MainWindow.Title,
                MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.No)
                return;

            if (_holidayCalendarDatesTable != null)
            {
                foreach (ChoTasksDataSet.CHO_HOLIDAY_CALENDAR_DATESRow calDateRow in _holidayCalendarDatesTable)
                    calDateRow.Delete();
            }

            row.Delete();
            e.Handled = true;

            if (HasChanges)
                UpdateCalendar();
        }

        private void AcceptChanges()
        {
            if (_holidayCalendarDatesTable != null)
                _holidayCalendarDatesTable.AcceptChanges();
            if (HolidayCalendarTable != null)
                HolidayCalendarTable.AcceptChanges();
        }

        protected override bool UpdateChanges()
        {
            if (!UpdateCalendar())
                return false;
            
            HolidayCalendarTable.AcceptChanges();
            return true;
        }

        protected override void RejectChanges()
        {
            if (_holidayCalendarDatesTable != null)
                _holidayCalendarDatesTable.RejectChanges();
            if (HolidayCalendarTable != null)
                HolidayCalendarTable.RejectChanges();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            SetAppointments();
        }

        protected override bool HasChanges
        {
            get
            {
                DataTable table = null;
                bool hasChanges = false;

                if (!hasChanges)
                {
                    table = HolidayCalendarTable != null ? HolidayCalendarTable.GetChanges() : null;
                    if (table != null && table.Rows.Count > 0)
                        hasChanges = true;
                }
                if (!hasChanges)
                {
                    table = _holidayCalendarDatesTable != null ? _holidayCalendarDatesTable.GetChanges() : null;
                    if (table != null && table.Rows.Count > 0)
                        hasChanges = true;
                }

                IsDataUpdated = hasChanges;
                return hasChanges;
            }
        }


        private void SetAppointments()
        {
            if (this.AptCalendar == null) return;
            //-- Use whatever function you want to load the MonthAppointments list, I happen to have a list filled by linq that has
            //   many (possibly the past several years) of them loaded, so i filter to only pass the ones showing up in the displayed
            //   month.  Note that the "setter" for MonthAppointments also triggers a redraw of the display.
            this.AptCalendar.MonthAppointments = _myAppointmentsList.FindAll(new System.Predicate<ChoAppointment>((ChoAppointment apt) => apt.StartTime != null && Convert.ToDateTime(apt.StartTime).Month == this.AptCalendar.DisplayStartDate.Month && Convert.ToDateTime(apt.StartTime).Year == this.AptCalendar.DisplayStartDate.Year));
        }

        void AptCalendar_AppointmentMouseMoved(DayBoxAppointmentControl source, object AppointmentId)
        {
            ChoTasksDataSet.CHO_HOLIDAY_CALENDAR_DATESRow dateRow = AppointmentId as ChoTasksDataSet.CHO_HOLIDAY_CALENDAR_DATESRow;
            if (dateRow == null)
                source.ToolTip = "Double click to add/edit holiday";
            else
                source.ToolTip = "Created By: {1}{0}Created Date: {2}{0}Modified By: {3}{0}Modified Date: {4}".FormatString(
                    Environment.NewLine, dateRow.CREATED_BY, dateRow.CREATED_DATE, dateRow.MODIFIED_BY, dateRow.MODIFIED_DATE);
        }

        private void AptCalendar_AppointmentDblClicked(object AppointmentId)
        {
            ChoTasksDataSet.CHO_HOLIDAY_CALENDAR_DATESRow dateRow = AppointmentId as ChoTasksDataSet.CHO_HOLIDAY_CALENDAR_DATESRow;
            if (dateRow == null) return;

            ChoMultilineInputWindow inputWindow = new ChoMultilineInputWindow(dateRow.IsCAL_DATE_DESCNull() ? String.Empty : dateRow.CAL_DATE_DESC, "Enter holiday name", "To remove holiday, empty the name");
            ChildWindow = inputWindow;
            bool? retValue = inputWindow.ShowDialog();
            if (retValue == null || !retValue.Value)
                return;

            string holidayDesc = inputWindow.InputText;
            if (!holidayDesc.IsNullOrWhiteSpace())
            {
                dateRow.CAL_DATE_DESC = holidayDesc;
                FindAppointment(dateRow.CAL_DATE).Subject = holidayDesc;
                SetAppointments();
            }
            else
            {
                ChoAppointment appt = FindAppointment(dateRow.CAL_DATE);
                if (appt != null)
                {
                    _myAppointmentsList.Remove(appt);
                    SetAppointments();
                    dateRow.Delete();
                }
            }
        }

        private void AptCalendar_DayBoxDoubleClicked(NewAppointmentEventArgs e)
        {
            if (lstHolidayCals.SelectedItem == null)
                return;

            ChoTasksDataSet.CHO_HOLIDAY_CALENDARSRow row = ((DataRowView)lstHolidayCals.SelectedItem).Row as ChoTasksDataSet.CHO_HOLIDAY_CALENDARSRow;
            if (row == null) return;

            ChoTasksDataSet.CHO_HOLIDAY_CALENDAR_DATESRow dateRow = null;
            if (!IsDayContainsAppointment(e.StartDate.Value, ref dateRow))
            {
                dateRow = _holidayCalendarDatesTable.NewCHO_HOLIDAY_CALENDAR_DATESRow();
                dateRow.CAL_DATE = e.StartDate.Value;
                dateRow.CAL_ID = row.CAL_ID;
            }

            //string holidayDesc = Microsoft.VisualBasic.Interaction.InputBox("Enter holiday name", "Holiday Information", dateRow.IsCAL_DATE_DESCNull() ? String.Empty : dateRow.CAL_DATE_DESC);
            ChoMultilineInputWindow inputWindow = new ChoMultilineInputWindow(dateRow.IsCAL_DATE_DESCNull() ? String.Empty : dateRow.CAL_DATE_DESC, "Enter holiday name", "To remove holiday, empty the name");
            ChildWindow = inputWindow;
            bool? retValue = inputWindow.ShowDialog();
            if (retValue == null || !retValue.Value)
                return;

            string holidayDesc = inputWindow.InputText;
            if (!holidayDesc.IsNullOrWhiteSpace())
            {
                dateRow.CAL_DATE_DESC = holidayDesc;
                dateRow.CREATED_BY = ChoUser.CurrentUser;
                dateRow.CREATED_DATE = DateTime.Now;
                dateRow.MODIFIED_BY = ChoUser.CurrentUser;
                dateRow.MODIFIED_DATE = DateTime.Now;
                if (dateRow.RowState == DataRowState.Detached)
                {
                    _holidayCalendarDatesTable.AddCHO_HOLIDAY_CALENDAR_DATESRow(dateRow);
                    _myAppointmentsList.Add(ToAppointmentObject(dateRow));
                }

                FindAppointment(dateRow.CAL_DATE).Subject = holidayDesc;
                SetAppointments();
            }


            SetAppointments();
        }

        private ChoAppointment FindAppointment(DateTime date)
        {
            return _myAppointmentsList.FindAll(new System.Predicate<ChoAppointment>((ChoAppointment apt) => apt.StartTime != null &&
                Convert.ToDateTime(apt.StartTime).Day == date.Day &&
                Convert.ToDateTime(apt.StartTime).Month == date.Month && Convert.ToDateTime(apt.StartTime).Year == date.Year)).FirstOrDefault();
        }

        private bool IsDayContainsAppointment(DateTime date, ref ChoTasksDataSet.CHO_HOLIDAY_CALENDAR_DATESRow dateRow)
        {
            ChoAppointment appt = _myAppointmentsList.FindAll(new System.Predicate<ChoAppointment>((ChoAppointment apt) => apt.StartTime != null &&
                Convert.ToDateTime(apt.StartTime).Day == date.Day &&
                Convert.ToDateTime(apt.StartTime).Month == date.Month && Convert.ToDateTime(apt.StartTime).Year == date.Year)).FirstOrDefault();
            if (appt == null)
                return false;

            dateRow = appt.AppointmentID as ChoTasksDataSet.CHO_HOLIDAY_CALENDAR_DATESRow;
            return true;
        }

        private void AptCalendar_DisplayMonthChanged(MonthChangedEventArgs e)
        {
            SetAppointments();
        }

        private void lstHolidayCals_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (lstHolidayCals.SelectedItem == null)
                return;

            ChoTasksDataSet.CHO_HOLIDAY_CALENDARSRow row = ((DataRowView)lstHolidayCals.SelectedItem).Row as ChoTasksDataSet.CHO_HOLIDAY_CALENDARSRow;
            if (row == null) return;

            //string holidayDesc = Microsoft.VisualBasic.Interaction.InputBox("Enter calendar description", "Calendar Information", row.IsCAL_DESCNull() ? String.Empty : row.CAL_DESC);
            ChoMultilineInputWindow inputWindow = new ChoMultilineInputWindow(row.IsCAL_DESCNull() ? String.Empty : row.CAL_DESC, "Enter calendar description");
            ChildWindow = inputWindow;
            bool? retValue = inputWindow.ShowDialog();
            if (retValue == null || !retValue.Value)
                return;

            string holidayDesc = inputWindow.InputText;
            if (holidayDesc.IsNullOrWhiteSpace() || (!row.IsCAL_DESCNull() && holidayDesc == row.CAL_DESC))
                return;

            row.CAL_DESC = holidayDesc;

            BindingOperations.GetBindingExpression(txtHolidayCalDesc, TextBox.TextProperty).UpdateTarget();
        }

        private void btnClone_Click(object sender, RoutedEventArgs e)
        {
            if (lstHolidayCals.SelectedItem == null)
                return;

            try
            {
                if (HasChanges)
                    UpdateCalendar();

                ChoTasksDataSet.CHO_HOLIDAY_CALENDARSRow row = ((DataRowView)lstHolidayCals.SelectedItem).Row as ChoTasksDataSet.CHO_HOLIDAY_CALENDARSRow;
                if (row == null) return;

                ChoInputWindow wnd = new ChoInputWindow("{0}Copy".FormatString(row.CAL_NAME), "Enter calendar name", CharacterCasing.Upper);
                bool? ret = wnd.ShowDialog();
                if (ret == null || !ret.Value)
                    return;

                string newCalName = wnd.InputText;

                if (newCalName.IsNullOrWhiteSpace())
                    return;
                int newTaskId = _dataModel.CloneCalendar(row.CAL_NAME, newCalName, ChoUser.CurrentUser, Environment.MachineName);
                Refresh();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Application.Current.MainWindow.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            if (lstHolidayCals.SelectedItem == null)
                return;

            ChoTasksDataSet.CHO_HOLIDAY_CALENDARSRow row = ((DataRowView)lstHolidayCals.SelectedItem).Row as ChoTasksDataSet.CHO_HOLIDAY_CALENDARSRow;
            if (row == null) return;
            
            if (HasChanges)
                UpdateCalendar();

            string fileName = row.CAL_NAME;
            try
            {
                ChoTasksDataSet dataSet = new ChoTasksDataSet();
                row.CREATED_DATE = DateTime.Now;
                row.CREATED_BY = ChoUser.CurrentUser;
                row.MODIFIED_DATE = DateTime.Now;
                row.MODIFIED_BY = ChoUser.CurrentUser;
                dataSet.CHO_HOLIDAY_CALENDARS.ImportRow(row);

                foreach (ChoTasksDataSet.CHO_HOLIDAY_CALENDAR_DATESRow holidayCalRow in _holidayCalendarDatesTable)
                {
                    holidayCalRow.CREATED_DATE = DateTime.Now;
                    holidayCalRow.CREATED_BY = ChoUser.CurrentUser;
                    holidayCalRow.MODIFIED_DATE = DateTime.Now;
                    holidayCalRow.MODIFIED_BY = ChoUser.CurrentUser;
                    dataSet.CHO_HOLIDAY_CALENDAR_DATES.ImportRow(holidayCalRow);
                }
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.FileName = fileName.IsNullOrWhiteSpace() ? "Document" : fileName; // Default file name
                dlg.DefaultExt = _defaultExt; // Default file extension
                dlg.Filter = _filter; // Filter files by extension

                // Show save file dialog box
                Nullable<bool> result = dlg.ShowDialog();

                // Process save file dialog box results
                if (result == true)
                {
                    // Save document
                    string filename = dlg.FileName;

                    ChoObject.Save(dataSet, filename);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Application.Current.MainWindow.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private void btnImport_Click(object sender, RoutedEventArgs e)
        {
            ChoTasksDataSet.CHO_TASK_SCHEDULESDataTable taskSchedulesTable = new ChoTasksDataSet.CHO_TASK_SCHEDULESDataTable();

            try
            {
                if (HasChanges)
                    UpdateCalendar();

                OpenFileDialog dlg = new OpenFileDialog();
                dlg.DefaultExt = _defaultExt; // Default file extension
                dlg.Filter = _filter; // Filter files by extension

                // Show save file dialog box
                Nullable<bool> result = dlg.ShowDialog();

                // Process save file dialog box results
                if (result == true)
                {
                    // Save document
                    string filename = dlg.FileName;
                    if (filename.IsNullOrWhiteSpace()) return;

                    ChoTasksDataSet dataSet = ChoObject.Deserialize<ChoTasksDataSet>(filename);

                    if (dataSet == null) return;

                    ChoTasksDataSet.CHO_HOLIDAY_CALENDARSDataTable calDataTable = new ChoTasksDataSet.CHO_HOLIDAY_CALENDARSDataTable();

                    foreach (ChoTasksDataSet.CHO_HOLIDAY_CALENDARSRow calRow in dataSet.CHO_HOLIDAY_CALENDARS)
                    {
                        calRow.CREATED_DATE = DateTime.Now;
                        calRow.CREATED_BY = ChoUser.CurrentUser;
                        calRow.MODIFIED_DATE = DateTime.Now;
                        calRow.MODIFIED_BY = ChoUser.CurrentUser;
                        calDataTable.ImportRow(calRow);
                    }

                    ChoTasksDataSet.CHO_HOLIDAY_CALENDAR_DATESDataTable calDatesDataTable = new ChoTasksDataSet.CHO_HOLIDAY_CALENDAR_DATESDataTable();
                    foreach (ChoTasksDataSet.CHO_HOLIDAY_CALENDAR_DATESRow calDateRow in dataSet.CHO_HOLIDAY_CALENDAR_DATES)
                    {
                        calDateRow.CREATED_DATE = DateTime.Now;
                        calDateRow.CREATED_BY = ChoUser.CurrentUser;
                        calDateRow.MODIFIED_DATE = DateTime.Now;
                        calDateRow.MODIFIED_BY = ChoUser.CurrentUser;
                        calDatesDataTable.ImportRow(calDateRow);
                    }

                    //Refresh();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, Application.Current.MainWindow.Title, MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }
    }
}